package com.whu.controller;

import com.whu.UVAProtocol.UVAConnection;
import com.whu.UVAProtocol.UVAServer;
import com.whu.aircraft.PictureInfo;
import com.whu.aircraft.R;
import com.whu.service.impl.PicInfoService;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
public class PicInfoController {
    @Autowired
    XmlRpcClient xmlRpcClient;
    PicInfoService picInfoService;
    public PicInfoController(PicInfoService picInfoService){
        this.picInfoService = picInfoService;
    }

    @RequestMapping("pic/getIllegalBuildingPairs/{uvaId}")
    public R getIllegalBuildingPairs(@PathVariable Integer uvaId){
        UVAConnection connection = UVAServer.uvaConnections.get(uvaId);
        return R.ok().put("illegalBuildingPairs",connection.getPicIllegalBuildingsPairs());

    }
    @RequestMapping("/pic/illegalBuildingObjective/{picId}")
    public String illegalBuildingObjective(@PathVariable long picId){
        return picInfoService.imageMatchRpc(picId);
    }
    @RequestMapping("/picInfo/test/{picId}")
    public void test(@PathVariable long picId){
        PictureInfo pictureInfo = picInfoService.selectById(picId);
        List<PictureInfo> pics = picInfoService.selectByTimeAndPos(pictureInfo);
        for(PictureInfo pic:pics){

            System.out.println(pic.getPicCreateTime()+" "+pic.getPicId()+" "+(pictureInfo.getLongitude()-pic.getLongitude())+" "+(pictureInfo.getLatitude()-pic.getLatitude()));
        }
    }
    @RequestMapping("/picInfo/test2")
    public void test2() throws MalformedURLException, XmlRpcException {
//        XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
//        clientConfig.setServerURL(new URL("http://127.0.0.1:9092/RPC"));
//        clientConfig.setEnabledForExceptions(true);
//        clientConfig.setEnabledForExtensions(true);
//        clientConfig.setConnectionTimeout(100*1000);
//        clientConfig.setReplyTimeout(100*1000);
//        XmlRpcClient client = new XmlRpcClient();
//        client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
//        client.setConfig(clientConfig);
        List<String> paths = new ArrayList<>();
        paths.add("/dd/ddd.jpg");
        paths.add("/pp/ppp");
        paths.add("/iri/j-fij");
        Object[] params = new Object[]{paths.toArray()};
        String res = (String)xmlRpcClient.execute("test",params);
        System.out.println(res);
    }
}
